package com.neuedu.corejava.ch12;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * <pre>
 * 20190814下午
 * 设计一个类，用于信息的保存于传输
 * </pre>
 * @author admin
 *
 */
public class TestStudent {
	
	/**
	 * 保存学生信息的文件
	 */
	private static final String FILE_NAME = "c:/student.data";

	// 编写一段代码，从键盘输入三个学生的信息，并保存到文件中
	public static void inputStudentInfo() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入三个学生的信息：");

		List<Student> list = new ArrayList<Student>();

		Student student = null;

		for (int i = 0; i < 2; i++) {
			
			int id=0;
			while(true){
				try {
					System.out.println("请输入学号(数字)");
					String idString = scanner.nextLine();
					 id = Integer.parseInt(idString);
					 break;
				} catch (NumberFormatException e) {
					// TODO: handle exception
				}
			}

			
			System.out.println("请输入名字：");
			String namaeString=scanner.nextLine();
			
			int age=0;
			while(true){
				try {
					System.out.println("请输入年龄");
					String ageString = scanner.nextLine();
					age = Integer.parseInt(ageString);
					break;
				} catch (NumberFormatException e) {
					// TODO: handle exception
				}
			}
			
			

			System.out.println("请输入性别");
			String sexString = scanner.nextLine();

			student = new Student(id, namaeString, age, sexString);

			list.add(student);
		}

		// 文件的定位
		File file = new File(FILE_NAME);
		FileOutputStream outputStream = null;
		ObjectOutputStream objectOutputStream=null;

		try {
			if (!file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();			
			}
			
			//操作文件-节点流
			outputStream=new FileOutputStream(file);
			
			//对流进行包装转换，处理流
			objectOutputStream=new ObjectOutputStream(outputStream);
			
			//使用对象流把一个java.lang.Object类的对象输出到一个流（网络流，管道流，文件流）
			objectOutputStream.writeObject(list);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(objectOutputStream!=null){
				try {
					objectOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(outputStream!=null){
				try {
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
	
	
	
	
	public static void readDataFormFile(){
		File file=new File(FILE_NAME);
		if(!file.exists()){
			System.out.println("文件找不到："+FILE_NAME);
			return;
		}
		
		FileInputStream fileInputStream=null;
		ObjectInputStream objectInputStream=null;
		
		try {
			fileInputStream=new FileInputStream(file);
			objectInputStream =new ObjectInputStream(fileInputStream);
			Object object=objectInputStream.readObject();
			List<Student> list=(List<Student>)object;//向下朔型
			
			for(Student student:list){
				System.out.println(student);
			}
			
		} catch (IOException | ClassNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(objectInputStream!=null){
				try {
					objectInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fileInputStream!=null){
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
	
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// 编写一段代码，从键盘输入三个学生的信息，并保存到文件中
		inputStudentInfo();

		// 编写一段代码，从文件中读取数据并还原为一组学生信息
		readDataFormFile();
	}

}
